package com.jack.leetcode.每日一题;

import java.util.Arrays;

public class 转变数组后最接近目标值的数组和 {

    public static void main(String[] args) {
        int[] arr = {4,9,3};
        System.out.println(findBestValue(arr,10));
    }

    public static int findBestValue(int[] arr, int target) {
        int n = arr.length;
        int sum  = 0;
        Arrays.sort(arr);
        for(int i = 0;i < n;i++){
            int x = (target - sum)/(n - i);
            if(x < arr[i]){
                double tar = (double)(target - sum)/(n - i);
                if(tar - x > 0.5){
                    return x+1;
                }else{
                    return x;
                }
            }
            sum +=arr[i];
        }
        return arr[n-1];

    }
}
